package customer

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/app/server"
	serviceCustomer "sieve_admin_server_userpass/app/service/customer"
)

var (
	_       util.API = (*UserAPI)(nil)
	UserSet          = wire.NewSet(wire.Struct(new(UserAPI), "*"))
)

type UserAPI struct {
	util.EmptyAPI
	UserSrv *serviceCustomer.UserSrv
}

func (a *UserAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "user",
			Title: "用户管理", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "queryLeaders", Title: "获取上级"},
				{Name: "queryRoles", Title: "获取用户角色"},
				{Name: "querySieveConfig", Title: "获取筛源配置"},
				{Name: "generatePasswd", Title: "生成密码"},
				{Name: "resetPasswd", Title: "重置密码"},
				{Name: "add", Title: "添加"},
				{Name: "del", Title: "删除"},
				{Name: "edit", Title: "编辑"},
				{Name: "enable", Title: "启用"},
				{Name: "disable", Title: "禁用"},
				{Name: "getAPIToken", Title: "获取API token"},
			},
			Order: 10,
		},
	}
}

func (a *UserAPI) Cron() {

	_ = a.UserSrv.Cron()
}

// 添加用户
func (a *UserAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("account"),
				contextx.NewVar("passwd"),
				contextx.NewVar("tag"),
				contextx.NewVar("role_id", true),
				contextx.NewVar("leader_id", true),
				contextx.NewVar("business_price_id", true), // 业务方案ID
			},
		)
	)
	return a.UserSrv.Create(context.Background(), uint64(ctx.Uid), reqArgs)
}

func (a *UserAPI) API_generatePasswd_item(ctx *server.Context) interface{} {

	return serviceCustomer.GeneratePasswd(8)
}

func (a *UserAPI) API_query_list(ctx *server.Context) interface{} {
	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("account"),
				contextx.NewVar("tag"),
				contextx.NewVar("leader_id", true),
				contextx.NewVar("role_id", true),
				contextx.NewVar("start", true),
				contextx.NewVar("end", true),
				contextx.NewVar("status", true),
				contextx.NewVar("self", true),
				contextx.NewVar("business_price_id", true),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	var includeSelf bool
	if v, ok := reqArgs["self"]; ok {
		includeSelf = v.(uint64) == 1
	}

	return a.UserSrv.Query(context.Background(), ctx.Uid, reqArgs, p, includeSelf)
}

func (a *UserAPI) API_queryLeaders_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("submit_type", true),  // 1：表示搜索框的上级查询，2：表示添加用户的时候的用户上级查询
				contextx.NewVar("will_role_id", true), // 将要添加的用户的角色ID
			},
		)
	)
	return a.UserSrv.QueryLeaders(context.Background(), ctx.Uid, reqArgs)
}

func (a *UserAPI) API_queryRoles_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("submit_type", true), // 是否忽略自己的角色
			},
		)
	)

	return a.UserSrv.QueryRoles(context.Background(), ctx.Uid, reqArgs)
}

func (a *UserAPI) API_querySieveConfig_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("submit_type", true), // 是否忽略自己的角色
			},
		)
	)

	return a.UserSrv.QueryRoles(context.Background(), ctx.Uid, reqArgs)
}

func (a *UserAPI) API_enable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.UserSrv.Enable(context.Background(), ctx.Uid, reqArgs)
}

func (a *UserAPI) API_disable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.UserSrv.Disable(context.Background(), ctx.Uid, reqArgs)
}

func (a *UserAPI) API_edit_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id"), // 一个或者多个 ,用 "|" 拼接
				contextx.NewVar("account"),
				contextx.NewVar("tag"),
				//contextx.NewVar("passwd"),
				contextx.NewVar("role_id", true),
				contextx.NewVar("leader_id", true),
				contextx.NewVar("business_price_id", true),
				contextx.NewVar("business_type_ids"), // sieve_source_config 表的主键ID,多个用 "|" 拼接
			},
		)
	)

	return a.UserSrv.Update(context.Background(), reqArgs)
}

func (a *UserAPI) API_resetPasswd_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("passwd"),
			},
		)
	)

	return a.UserSrv.ResetPasswd(context.Background(), uint64(ctx.Uid), reqArgs)
}

func (a *UserAPI) API_del_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.UserSrv.Delete(context.Background(), reqArgs)
}

func (a *UserAPI) API_getAPIToken_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.UserSrv.GetAPIToken(context.Background(), reqArgs)
}

func (a *UserAPI) API_querySieveConfig_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	var uid uint64
	if v, ok := reqArgs["id"]; ok {
		uid = v.(uint64)
	}
	return a.UserSrv.QuerySieveConfig(context.Background(), uid)
}
